/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControlLayer;

import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

/**
 *
 * @author Claus
 */
public class CtrSupplier
{

	/** Creates a new instance of CtrSupplier */
	public CtrSupplier()
	{
	}

	public ArrayList<Supplier> getAllSuppliers()
	{
		IFDBSupplier dbSup = new DBSupplier();
		ArrayList<Supplier> allSup = new ArrayList();
		allSup = dbSup.getAllSuppliers(false);
		return allSup;
	}

	public Supplier findByName(String name)
	{
		IFDBSupplier dbSup = new DBSupplier();
		return dbSup.searchSupplierName(name, true);
	}

	public Supplier findByID(int id)
	{
		IFDBSupplier dbSup = new DBSupplier();
		return dbSup.findSupplier(id, true);
	}

	public int updateSupplier(String name, String address, String cCode, String phoneNo, String email)
	{
		IFDBSupplier dbSup = new DBSupplier();
		Supplier sup = new Supplier();
		sup.setName(name);
		sup.setAddress(address);
		sup.setCountryCode(cCode);
		sup.setPhoneNo(phoneNo);
		sup.setEmail(email);

		IFDBCountry dbCount = new DBCountry();
		try
		{
			String country = dbCount.getCountryName(cCode);
			sup.setCountry(country);
		}
		catch(Exception e)
		{
			System.out.println("Ugyldig landekode");
		}

		return dbSup.updateSupplier(sup);
	}

	public void insertSupplier(Supplier supObj)
	{
		IFDBSupplier dbSup = new DBSupplier();
		dbSup.insertSupplier(supObj);
	}

	public void deleteSupplier(int id)
	{
		IFDBSupplier dbSup = new DBSupplier();
		dbSup.deleteSupplier(id);
	}

	public Supplier createSupplier(String name, String address, String countryCode, String phoneNo, String email)
	{
		String country;

		IFDBCountry dbCount = new DBCountry();
		Supplier sup;


		try
		{
			country = dbCount.getCountryName(countryCode);
			sup = new Supplier();
			sup.setName(name);
			sup.setAddress(address);
			sup.setCountryCode(countryCode);
			sup.setCountry(country);
			sup.setPhoneNo(phoneNo);
			sup.setEmail(email);
		}
		catch(Exception e)
		{
			sup = null;
			System.out.println("Ugyldig landekode");
		}

		return sup;
	}
}
